package com.hugh.edu.aop;

import com.hugh.edu.util.MapCacheUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.Objects;

/**
 * @Author: Hugh
 * @Date: 2020/5/18
 */
public class LoginIntercepter implements HandlerInterceptor {

    private static String USER_NAME = "username";
    private static String PASS_WORD = "password";
    private static String LOGIN_URL = "http://localhost:8090/security/login";
    private static String LOGIN_PATH = "/security/login/submit";

    @Autowired
    private MapCacheUtil mapCacheUtil;
    /**
     * 会在handler方法业务逻辑执行之前执行
     * 往往在这里完成权限校验工作
     * @param request
     * @param response
     * @param handler
     * @return  返回值boolean代表是否放行，true代表放行，false代表中止
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        request.setCharacterEncoding("UTF-8");
        String requestUri = request.getRequestURI();
        if(requestUri.contains("scripts/jquery-1.9.1.min.js")){
            return true;
        }
        if("/security/login".equals(requestUri)){
            return true;
        }
        Map<String, String[]> parameterMap = request.getParameterMap();
        if(Objects.isNull(parameterMap) || (!parameterMap.containsKey(USER_NAME))){
            //跳转到登陆页面
            response.sendRedirect(LOGIN_URL);
            return false;
        }
        String name = parameterMap.get(USER_NAME)[0];
        if(!"admin".equals(name)){
            //跳转到登陆页面
            response.sendRedirect(LOGIN_URL);
            return false;
        }
        //第一次登陆的情况
        if(LOGIN_PATH.equals(requestUri) && !StringUtils.isEmpty(name)){
            if(parameterMap.containsKey(PASS_WORD) && !StringUtils.isEmpty(parameterMap.get(PASS_WORD)[0])){
                    return true;
            }
            return true;
        }
        //其他
        if(StringUtils.isEmpty(mapCacheUtil.get(name)) ||
                (parameterMap.containsKey(PASS_WORD) && !parameterMap.get(PASS_WORD)[0].equals(mapCacheUtil.get(name)))){
                response.sendRedirect(LOGIN_URL);
                return false;
        }
        return true;
    }


    /**
     * 会在handler方法业务逻辑执行之后尚未跳转页面时执行
     * @param request
     * @param response
     * @param handler
     * @param modelAndView  封装了视图和数据，此时尚未跳转页面呢，你可以在这里针对返回的数据和视图信息进行修改
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("LoginIntercepter postHandle do nothing");
    }

    /**
     * 页面已经跳转渲染完毕之后执行
     * @param request
     * @param response
     * @param handler
     * @param ex  可以在这里捕获异常
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("LoginIntercepter afterCompletion do nothing");
    }
}
